package com.kancy.vip.pattern.singleton.lazy;

public class LazyInnerClassSingleton {
    private LazyInnerClassSingleton() {
        if(LazyHolder.LAZY != null){
            throw new RuntimeException("防止反射入侵");
        }
    }

    public static LazyInnerClassSingleton getInstance() {
        return LazyHolder.LAZY;
    }

    /**
     * 防止反序列化破坏
     * @return
     */
    private Object readResolve(){
        return LazyHolder.LAZY;
    }

    /**
     * 利用静态内部类只有在使用时，才会进行类加载的机制实现赖汉式
     */
    private static class LazyHolder{
        private static final LazyInnerClassSingleton LAZY = new LazyInnerClassSingleton();
    }
}